$(function () {
    // 1.1 渲染动态页面引入
    $('#inputCover').on('change', function () {
        const file = this.files[0]
        const src = URL.createObjectURL(file)
        $('#coverimg').prop('src',src)
    })
    // 1.2 文章类别下拉框
    function getArticleCategory() {
        $.ajax({
            url: 'http://localhost:8080/api/v1/admin/category/list',
            headers: {
                Authorization:localStorage.getItem('token')
            },
            success: function (res) {
                console.log(res);
                if (res.code === 200) { 
                    const html = res.data.reduce((html, value) => html += `<option value='${value.id}'>${value.name}</option>`, `<option value=''>所有分类</option>`)
                    $('#category').html(html)
                }
                
            }
            
        })
    }

    getArticleCategory()

    //1.3发布日期
    layui.use('laydate', function(){
        const laydate = layui.laydate;
        
        //执行一个laydate实例
        laydate.render({
          elem: '#articleDate' //指定元素
        });
    });
    
    //1.4 富文本
    tinymce.init({
        selector: '#articleContent',
        language:'zh_CN'
    });
    
    //文章发表按钮
    $('.btn-edit').on('click', function () {
        //数据用formdata
        let formdata = new FormData($('#form')[0])
        
        //拼接formdata
        formdata.append('content', tinyMCE.editors['articleContent'].getContent())
        formdata.append('state', '已发布')
        
        formdata.forEach(function (val, key) {
            console.log(key,val);
        })

        $.ajax({
            url: 'http://localhost:8080/api/v1/admin/article/publish',
            type: 'post',
            headers: {
                Authorization:localStorage.getItem('token')
            },
            processData: false,
            contentType:false,
            data: formdata,
            success(res) {
                if (res.code === 200) {
                    console.log(res);
                    location.href = 'article_list.html'
                }
                else {
                    console.log('失败了');
                    alert(res.msg)
                }
            }
            
        })
    })

    //文章草稿按钮
    $('.btn-draft').on('click', function () {
        //数据用formdata
        let formdata = new FormData($('#form')[0])
        
        //拼接formdata
        formdata.append('content', tinyMCE.editors['articleContent'].getContent())
        formdata.append('state', '')
        
        formdata.forEach(function (val, key) {
            console.log(key,val);
        })

        $.ajax({
            url: 'http://localhost:8080/api/v1/admin/article/publish',
            type: 'post',
            headers: {
                Authorization:localStorage.getItem('token')
            },
            processData: false,
            contentType:false,
            data: formdata,
            success(res) {
                if (res.code === 200) {
                    console.log(res);
                    location.href = 'article_list.html'
                }
                else {
                    console.log('失败了');
                    alert(res.msg)
                }
            }
            
        })
    })

})